package cn.mw.cmdb.trigger.controller;

import cn.mw.cmdb.api.MwTrigger;
import cn.mw.cmdb.api.MwTriggerSearchParam;
import cn.mw.cmdb.trigger.*;
import cn.mw.cmdb.trigger.service.impl.MwTriggerServiceImpl;
import cn.mw.cmdb.view.ViewManager;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.page.MWPage;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import cn.mw.microMonitorCommon.entity.alertDto.PageData;
import cn.mw.microMonitorCommon.entity.notify.NotifyLogVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/triggerManage")
@Controller
@Tag(name = "触发器管理")
@Slf4j
public class TriggerController extends BaseApiService {
    private MwTriggerServiceImpl mwTriggerService;

    private ViewManager viewManager;

    public TriggerController(MwTriggerServiceImpl mwTriggerService ,ViewManager viewManager){
        this.mwTriggerService = mwTriggerService;
        this.viewManager = viewManager;
    }

    @PostMapping("/listTrigger")
    @ResponseBody
    @Operation(summary = "触发器列表")
    public ResponseBase listTrigger(@RequestBody MwTriggerSearchParam mwTriggerSearchParam) throws Exception{
        List<MwTrigger> ret = mwTriggerService.listAllTrigger(mwTriggerSearchParam);
        return setResultSuccess(ret);
    }

    @PostMapping("/saveNotify")
    @ResponseBody
    @Operation(summary = "保存提醒")
    public ResponseBase saveNotify(@RequestBody MwCmdbNotifyParam param) throws Exception{
        MwCmdbNotifyParam ret = mwTriggerService.saveNotify(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/deleteNotify")
    @ResponseBody
    @Operation(summary = "删除提醒")
    public ResponseBase deleteNotify(@RequestBody MwCmdbNotifyDelParam param) throws Exception{
        MwCmdbNotifyDelParam ret = mwTriggerService.deleteNotify(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/selectNotify")
    @ResponseBody
    @Operation(summary = "查看提醒")
    public ResponseBase selectNotify(@RequestBody MwCmdbNotifyParam param) throws Exception{
        MwCmdbNotifyParam ret = mwTriggerService.selectNotify(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/listNotify")
    @ResponseBody
    @Operation(summary = "查看提醒")
    public ResponseBase listNotify(@RequestBody SearchParam param) throws Exception{
        MWPage ret = mwTriggerService.listNotify(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/listDropdown")
    @ResponseBody
    @Operation(summary = "下拉框选项")
    public ResponseBase listDropdown() throws Exception{
        MwCmdbNotifyDropdown ret = mwTriggerService.listDropdown();
        return setResultSuccess(ret);
    }

    @PostMapping("/listFieldDropdown")
    @ResponseBody
    @Operation(summary = "下拉框选项")
    public ResponseBase listFieldDropdown(@RequestBody MwNofityScope scope) throws Exception{
        Map ret = mwTriggerService.listFieldDropdown(scope);
        return setResultSuccess(ret);
    }

    @PostMapping("/listNotifyByScope")
    @ResponseBody
    @Operation(summary = "查看范围数据关联的提醒")
    public ResponseBase listNotifyByScope(@RequestBody MwNotifyScopeSearchParam searchParam) throws Exception {
        List<MwNotifyByScopeView> list = mwTriggerService.listNotifyByScope(searchParam);
        Object ret = viewManager.getListViewObject(list, MwNotifyByScopeView.class, searchParam, null);
        return setResultSuccess(ret);
    }

    @PostMapping("/listNotifyLog")
    @ResponseBody
    @Operation(summary = "查看历史提醒")
    public ResponseBase listNotifyLog(@RequestBody SearchParam param) throws Exception {
        PageData<NotifyLogVO> ret = mwTriggerService.listNotifyLog(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/deleteNotifyLog")
    @ResponseBody
    @Operation(summary = "批量删除历史提醒")
    public ResponseBase deleteNotifyLog(@RequestBody MwCmdbNotifyLogDelParam param) throws Exception {
        return setResultSuccess(mwTriggerService.deleteNotifyLog(param));
    }

}
